package com.woniuxy.trade.service;

import com.woniuxy.common.exceptions.BusinessException;
import com.woniuxy.common.result.enums.impl.BusinessCode;
import com.woniuxy.common.utils.TokenUtil;
import com.woniuxy.common.vo.TokenVo;
import com.woniuxy.trade.entity.Room;
import com.woniuxy.trade.mapper.RoomMapper;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class RoomService {
    @Autowired
    RoomMapper roomMapper;
    @Autowired
    RabbitTemplate rabbitTemplate;
    public void countdown(int roomId) {
        //判断房间状态是否为正在竞拍
        Room room = roomMapper.selectByPrimaryKey(roomId);
        if(room.getStatus() != 1){
            System.err.println("房间状态异常");
            throw new BusinessException(BusinessCode.ROOM_STATUS_ERR);
        }
        //开始倒计时，发送MQ消息
        Date startdate = room.getStartdate();
        Date enddate = room.getEnddate();
        long time = enddate.getTime() - startdate.getTime();
        System.err.println("竞拍倒计时：" + time);
        rabbitTemplate.convertAndSend("NORMAL_EXCHANGE","NORMAL_ROUTING_KEY",room,
                message -> {
                    message.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
                    message.getMessageProperties().setExpiration(time+"");
                    return message;
                });
        //是否存在redis中
    }
}
